package com.core.filedb;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.core.common.Rog;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 文件引擎
 */
public class DataEngineMain {
    public static void main(String[] args) {

        ExecutorService es = Executors.newFixedThreadPool(10);
        try {


            for (int i = 0; i < 1000; i++) {
                String tableName = "test_00" + i % 10;
                es.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            for (int i = 0; i < 10; i++) {
                                Test01Bean bean = new Test01Bean();
                                bean.setId(IdUtil.fastUUID());
                                bean.setCode("xx" + i);
                                bean.setCreateTime(new Date());
                                DataEngine.insert(tableName, bean);

                            }
                            List<Test01Bean> dbList = DataEngine.data(tableName, Test01Bean.class);
                            print(dbList, "插入十条数据");
                            for (int i = 0; i < 3; i++) {
                                dbList.get(i).setCode("00" + i);
                            }
                            dbList = DataEngine.data(tableName, Test01Bean.class);
                            print(dbList, "修改前三条");
                            for (int i = 0; i < 3; i++) {
                                DataEngine.delete(tableName, dbList.get(i).getId());
                            }
                            try {
                                Thread.sleep(Double.valueOf(1000 * Math.random()).intValue());
                            } catch (Exception ex) {

                            }
                            dbList = DataEngine.data(tableName, Test01Bean.class);
                            print(dbList, "删除前三条");
                            for (Test01Bean bean : dbList) {
                                DataEngine.delete(tableName, bean.getId());
                            }
                            dbList = DataEngine.data(tableName, Test01Bean.class);
                            print(dbList, "删除全部数据");

                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }


                    }
                });
                try {
                    Thread.sleep(100);
                } catch (Exception ex) {

                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage());
        }
    }

    public static Rog log = Rog.build("test", "filedb");

    private static void print(List<Test01Bean> dataList, String name) {
        if (dataList != null) {
            log.log(Thread.currentThread().getId() + "*******************************" + name);
            for (Test01Bean baseEntity : dataList) {
                log.log(baseEntity.getId() + " " + baseEntity.getCode() + "  " + DateUtil.formatDateTime(baseEntity.getCreateTime()));
            }
            log.log(Thread.currentThread().getId() + "*******************************" + name);
        }
    }

}
